package com.wrs.examsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Student;
import com.wrs.examsystem.entity.bean.StudentInformation;
import com.wrs.examsystem.service.StudentService;
import com.wrs.examsystem.util.wheel.StudentUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/examsystem/student")
public class StudentController {
    @Resource
    StudentService studentService;
    /**
     * 根据班级查看所有的学生
     * @param studentClass 班级号
     * @return 返回查询到的所有学生存在data里面
     */
    @GetMapping("findStudents")
    public R findStudents(@RequestParam String studentClass){
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_class",studentClass);
        List<Student> students = studentService.list(studentQueryWrapper);
//        StudentInformation studentInformation;
        List<StudentInformation> studentInformationList = new ArrayList<>();

        for (Student student : students) {

            studentInformationList.add(StudentUtil.studentInformation(student));
        }
        return R.ok().data(studentInformationList);
    }

    /**
     * 核验
     * @param student 修改密码时用户所输入的信息
     * @return
     */
    @PostMapping("checkUser")
    public R checkUser(@RequestBody Student student){
        return studentService.checkUser(student);
    }

    /**
     * 根据学号查询成绩
     * @param studentNumber
     * @return
     */
    @GetMapping("findTotalMark")
    public R findTotalMark(@RequestParam("studentNumber") String studentNumber,@RequestParam("testId") Integer testId){
//        判断学号是否合法
        String CHECK_STUDENT_NUMBER ="^\\d{11}$";
        if (!studentNumber.matches(CHECK_STUDENT_NUMBER)){
            return R.error().message("学号不合法");
        }

        return R.ok().data(studentService.findTotalMark(studentNumber,testId));
    }

    /**
     * 备用
     * @param classes
     * @return
     */
    @GetMapping("findStudentsMark")
    public R findStudentsMark(@RequestParam("classes") String classes){
        return null;
    }

    /**
     * 根据学号删除学生
     * @param studentNumber
     * @return
     */
    @DeleteMapping("deleteByStudentNumber")
    public R deleteByStudentNumber(@RequestParam("studentNumber") String studentNumber){
//        判断学号是否合法
        String CHECK_STUDENT_NUMBER ="^\\d{11}$";
        if (!studentNumber.matches(CHECK_STUDENT_NUMBER)){
            return R.error().message("学号不合法");
        }
        Boolean studentByStudentNumber = studentService.findStudentByStudentNumber(studentNumber);
        if (!studentByStudentNumber){
            return R.error().message("不存在该学生");
        }
        return studentService.deleteByStudentNumber(studentNumber);
    }
}

